import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * This class creates  an interface for an agency or an Employer
 * Final Project for TCSS 445.
 * 
 * @author Sanjeev Kamboj
 */
@SuppressWarnings("serial")
public class ShowPanel extends JPanel {
	
	private User my_user;
	private SqlDao my_dao;

	private JLabel my_pick_label;
	
	private JComboBox<String> my_pick;
	private JTextArea my_text;


	private JFrame my_frame;
	private JButton cancel_button;
	private JButton select_button;
	
	private ListenJobSelect job_listener;
	private ListenEventSelect event_listener;
	
	
	public ShowPanel(final JFrame the_frame, final SqlDao the_dao, final User user) {
		super(new BorderLayout());
		
		my_frame = the_frame;
		my_dao = the_dao;
		my_user = user;
		
		my_pick = new JComboBox<String>();
		my_text = new JTextArea();
		
		my_pick_label = new JLabel("Pick a Job Posting: ");
		final JPanel top_panel = new JPanel(new FlowLayout());
		
		top_panel.add(my_pick_label);
		top_panel.add(my_pick);
		
        add(top_panel, BorderLayout.NORTH);
        add(my_text, BorderLayout.CENTER);
        
		//Layout the text fields in a panel.
        JPanel buttonPane = new JPanel(new FlowLayout());
        select_button = new JButton("Select");
        cancel_button = new JButton("Cancel");
        
		cancel_button.addActionListener(new ListenCancel());
		//select_button.addActionListener(new ListenSelect());
		
		buttonPane.add(select_button);
        buttonPane.add(cancel_button);
        
        
        add(buttonPane, BorderLayout.SOUTH);
        

		job_listener = new ListenJobSelect();
		event_listener = new ListenEventSelect();
	}
	protected void fillJobs() {
		ResultSet set = my_dao.getJobPosting(my_user.getID());
		
		select_button.removeActionListener(job_listener);
		select_button.removeActionListener(event_listener);
		
		select_button.addActionListener(job_listener);
		
		try {
			while(set.next()) {
				String str = "";
				
				str = set.getString(1) + " " + set.getString(2) + " " +
						     set.getString(4) + " " + set.getString(5) + " " +
						     set.getString(7);
				
				my_pick.addItem(str);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	protected void fillEvents() {
		ResultSet set = my_dao.getEvent(my_user.getID());
		
		select_button.removeActionListener(job_listener);
		select_button.removeActionListener(event_listener);
		
		select_button.addActionListener(event_listener);
		
		try {
			while(set.next()) {
				String str = "";
				
				str = set.getString(1) + " " + set.getString(2) + " " +
						     set.getString(3) + " " + set.getString(6);
				
				my_pick.addItem(str);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private class ListenJobSelect implements ActionListener {
		 public void actionPerformed(ActionEvent e){
			 	//get the job id from the selected one
			 	final int index = my_pick.getSelectedIndex(); 
			 	
			 	if(index != -1) {
				 	String selected = my_pick.getItemAt(index);
				 	
				 	final int job_id = Integer.parseInt(selected.split(" ")[0]);
				 	ResultSet result = my_dao.getOneJobPost(job_id);
				 	String result_str = "";
				 	
				 	try {
						while(result.next()) {
							result_str = "Position Title:  " + result.getString(2) + "\n\n" +
						 			 	 "Industry:  " + result.getString(3) + "\n\n" +
						 			 	 "Open Date:  " + result.getString(4) + "\n\n" +
						 			 	 "Close Date:  " + result.getString(5) + "\n\n" +
						 			 	 "Minimum Qualifications:  " + result.getString(6) + "\n\n" +
						 			 	 "Job Location:  " + result.getString(7) + "\n\n" +
						 			 	 "Minimum Salary:  " + result.getString(8) + "\n\n" +
						 			 	 "Maximum Salary:  " + result.getString(9) + "\n\n" +
						 			 	 "How to Apply:  " + result.getString(10) + "\n";
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				 
				 	
				 	my_text.setText(result_str);		 
			 	}
			 	
	            my_frame.repaint();
	            my_frame.setVisible(true);
	    }
	}
	private class ListenEventSelect implements ActionListener {
		 public void actionPerformed(ActionEvent e){
			 	//get the job id from the selected one
			 	final int index = my_pick.getSelectedIndex(); 
			 	
			 	if(index != -1) {
			 		
			 	
				 	String selected = my_pick.getItemAt(index);
				 	
				 	final int event_id = Integer.parseInt(selected.split(" ")[0]);
				 	ResultSet result = my_dao.getOneEvent(event_id);
				 	String result_str = "";
				 	
				 	try {
						while(result.next()) {
							result_str = "Type:  " + result.getString(2) + "\n\n" +
						 			 	 "Audience:  " + result.getString(3) + "\n\n" +
						 			 	 "Objective:  " + result.getString(4) + "\n\n" +
						 			 	 "Address:  " + result.getString(5) + "\n\n" +
						 			 	 "Date:  " + result.getString(6) + "\n\n" +
						 			 	 "Time:  " + result.getString(7) + "\n\n" +
						 			 	 "Information:  " + result.getString(8) + "\n\n";
						}
						
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				 
				 	
				 	my_text.setText(result_str);		 
			 	} 
	            my_frame.repaint();
	            my_frame.setVisible(true);
	    }
	}
	
	private class ListenCancel implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	            my_frame.remove(ShowPanel.this);
	            
	            my_frame.repaint();
	            my_frame.setVisible(true);
	    }
	}
}
